import { Component } from '@angular/core';

import { NavController, ViewController, AlertController} from 'ionic-angular';

import { Storage } from '@ionic/storage';

import { Common } from "../../../providers/common";

import _ from "lodash";
import {CoinService} from "./coin.service";

@Component({
	selector: 'modal-ipm-coin-delete',
	templateUrl: 'delete.modal.html',
  providers: [CoinService]
})
export class IpmCoinDeleteModal {
	moa_page_name: any = "modal-ipm-coin-delete";
  moa_page_explain: any = "货币基金试算 - 批量删除模拟情景";

  dataList = [];
  dataListSource = [];

  isSelectAll = false;

  isNoMoreData = false;

  keyWord = "";

	constructor(
		public navCtrl: NavController,
    public common: Common,
    public viewCtrl: ViewController,
    public storage: Storage,
    public coinService: CoinService,
    public alertCtrl: AlertController
	) {

  }

  updateList() {
	  if (this.keyWord) {
	    this.dataList = _.filter(this.dataList, data => {
	      return data["entry"]["province_city"].indexOf(this.keyWord) > -1;
      });
    } else {
	    this.dataList = this.dataListSource;
    }
  }

  selectAll() {
	  if (this.isSelectAll) {
      _.forEach(this.dataList, fund => {
        fund["selected"] = true;
      });
    } else {
      _.forEach(this.dataList, fund => {
        fund["selected"] = false;
      });
    }
  }

  ionViewDidEnter() {
    this.dataList = this.viewCtrl.getNavParams().get("dataList");
    if (this.dataList && this.dataList.length <= 0) {
      this.isNoMoreData = true;
    }

    if (this.dataList) {
      _.forEach(this.dataList, data => {
        data["selected"] = false;
      });
    }
  }

  closeModal() {
    this.viewCtrl.dismiss("close");
  }

  completeModal() {
    let list = _.filter(this.dataList, {selected: true});
    if (list && list.length > 0) {
      let alert = this.alertCtrl.create({
        title: "提示",
        message: "确定要删除选中模拟情景,该模拟情景下的试算方案也将全部删除？",
        buttons: [{
          text: '取消',
          role: 'cancel',
          handler: () => {

          }
        }, {
          text: "确定",
          handler: () => {
            let plan_no_list = _.map(list, "l_plan_no");
            this.coinService.deleteSimulatedScene(_.toString(plan_no_list)).subscribe(deleteResult => {
              if (deleteResult && deleteResult[0] && deleteResult[0]["l_err_code"] === "0") {
                let errorAlert = this.alertCtrl.create({
                  title: "提示",
                  message: "模拟情景已删除",
                  buttons: [
                    {
                      text: "确定",
                      role: 'cancel',
                      handler: () => {
                        this.viewCtrl.dismiss(true);
                      }
                    }
                  ]
                });
                errorAlert.present();
              } else {
                this.common.alertMessage("删除模拟情景出错");
              }
            });
          }
        }]
      });
      alert.present();
    } else {
      this.common.alertMessage("没有选择模拟情景")
    }
  }


}

